from rest_framework.views import APIView, Response
from ..serializers.remind_serializers import RemindSerializer,DoneListSerializer
from remind.models import RemindModel,DoneListModel
from send.worker import send_worker

class Remind(APIView):
    def get(self, request):
        context = dict()
        context['err_code'] = 0
        data = request.GET
        user = request.user
        if user.is_anonymous:
            context['err_code'] = 1001
            context['error'] = "您还未登录"
            return Response(context)
        id = data.get('id')
        if id is None:
            context['err_code'] = 2001
            context['error'] = "请求参数不正确"
            return Response(context)
        query = RemindModel.objects.filter(id=id)
        if not query.exists():  # 请求数据不存在
            context['err_code'] = 3004
            context['error'] = "无法找到您要的数据"
            return Response(context)
        remind = query.first()
        is_owner = (remind.owner == user)
        context['data']=RemindSerializer(remind).data

        is_member = False
        for group in remind.groups.all():
            if not is_member:
                for member in group.members.all():
                    if user == member:
                        is_member = True
                        break
        if is_owner:
            pass
        else:
            if is_member:
                del context['data']['groups']
            else:
                del context['data']
                context['err_code'] = 3003
                context['error'] = "您无权查看此内容"
        return Response(context)

    def post(self, request):
        context = dict()
        context['err_code'] = 0
        data = request.POST
        user = request.user
        if user.is_anonymous:
            context['err_code'] = 1001
            context['error'] = "您还未登录"
            return Response(context)
class DoneList(APIView):
    def get(self, request):
        context = dict()
        context['err_code'] = 0
        data = request.GET
        user = request.user
        if user.is_anonymous:
            context['err_code'] = 1001
            context['error'] = "您还未登录"
            return Response(context)
        uid = data.get('uid')
        rid=data.get('rid')
        if uid is None or rid is None:
            context['err_code'] = 2001
            context['error'] = "请求参数不正确"
            return Response(context)
        query = DoneListModel.objects.filter(user_id=uid,remind_id=rid)
        if not query.exists():  # 请求数据不存在
            context['err_code'] = 3004
            context['error'] = "无法找到您要的数据"
            return Response(context)
        done_list = query.first()
        context['data']=DoneListSerializer(done_list).data
        return Response(context)
class Send(APIView):
    def get(self,request):
        context = dict()
        context['err_code'] = 0
        data = request.GET
        user = request.user
        if user.is_anonymous:
            context['err_code'] = 1001
            context['error'] = "您还未登录"
            return Response(context)
        rid = data.get('rid')
        if rid is None:
            context['err_code'] = 2001
            context['error'] = "请求参数不正确"
            return Response(context)
        query =RemindModel.objects.filter(id=rid)
        if not query.exists():  # 请求数据不存在
            context['err_code'] = 3004
            context['error'] = "无法找到您要的数据"
            return Response(context)
        remind=query.first()
        if request.user != remind.owner:
            context['err_code'] = 3003
            context['error'] = "您无权查看此内容"
            return Response(context)
        send_worker(remind)
        return Response(context)